package com.free.framework.business;

import com.free.framework.BuildConfig;
import com.free.framework.R;
import com.free.framework.lib.utils.ContextHolder;
import com.free.framework.lib.utils.LogHelper;
import com.free.framework.lib.utils.NetworkUtil;
import com.free.framework.lib.utils.ResMgr;
import com.free.framework.mvp.IBaseMvpView;

import rx.Subscriber;

/**
 * @author 李博
 * @date 2016年10月24日
 * @desc
 */
public abstract class NetSubscriber<T> extends Subscriber<T> {

    private IBaseMvpView mBaseMvpView;
    private boolean isShowLoadingView;
    private boolean isCancelable;


    public NetSubscriber(IBaseMvpView mBaseMvpView) {
        this.mBaseMvpView = mBaseMvpView;
        this.isShowLoadingView = true;
        this.isCancelable = true;
    }

    public NetSubscriber(IBaseMvpView mBaseMvpView, boolean isShowLoadingView) {
        this.mBaseMvpView = mBaseMvpView;
        this.isShowLoadingView = isShowLoadingView;
        this.isCancelable = true;
    }

    public NetSubscriber(IBaseMvpView mBaseMvpView, boolean isShowLoadingView, boolean isCancelable) {
        this.mBaseMvpView = mBaseMvpView;
        this.isShowLoadingView = isShowLoadingView;
        this.isCancelable = isCancelable;
    }

    @Override
    public void onStart() {
        super.onStart();

        if (!NetworkUtil.isConnected(ContextHolder.getContext())) {
            mBaseMvpView.showToast(ResMgr.getString(R.string.net_unavailable));
            onCompleted();
        } else {
            if (isShowLoadingView && null != mBaseMvpView) {
                mBaseMvpView.showLoadingView(isCancelable);
            }
        }
    }


    @Override
    public void onError(Throwable e) {

        if (null == mBaseMvpView) {
            return;
        }
        mBaseMvpView.hideLoadingView();
        mBaseMvpView.hideRefreshingView();

        if (e instanceof ResponseException) {
            ResponseException responseException = (ResponseException) e;
            if (responseException.code == ResponseExceptionHandler.ERROR.SSL_ERROR) {
                mBaseMvpView.showSSLError();
            } else {
                if (BuildConfig.DEBUG) {
                    mBaseMvpView.showToast("code = " + responseException.code + " msg = " + responseException.message);
                } else {
                    mBaseMvpView.showToast(ResMgr.getString(R.string.net_busy));
                }
            }
            LogHelper.e("NetSubscriber -> " + responseException.message);
        } else if (e instanceof ServerException) {
            onError((ServerException) e);
        } else {
            LogHelper.e("NetSubscriber -> " + e.getMessage());
        }

    }

    @Override
    public void onCompleted() {

        if (null == mBaseMvpView) {
            return;
        }
        mBaseMvpView.hideLoadingView();
        mBaseMvpView.hideRefreshingView();
    }

    /**
     * @param e
     * @desc 服务器定义的异常
     */
    protected void onError(ServerException e) {
        mBaseMvpView.showToast(e.message);
    }

}
